package com.example.service.impl;

import com.example.dao.CourseInfoDTO;
import com.example.dao.CourseQuery;
import com.example.entity.CourseDescription;
import com.example.entity.CourseInfo;
import com.example.entity.CourseWeb;
import com.example.entity.EduCourse;
import com.example.exception.Exception;
import com.example.mapper.CourseInfoMapper;
import com.example.service.*;
import com.example.utils.ResultEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 */
@Service
@Slf4j
public class CourseInfoServiceImpl extends ServiceImpl<CourseInfoMapper, CourseInfo> implements CourseInfoService {

    @Autowired
    private CourseDescriptionService CourseDescriptionService;
    @Autowired
    private ChapterService ChapterService;


    /********************************以下为后台管理接口*************************************************/

    @Override
    public List<CourseInfo> getMemberCourseByAuth(String id) {
        return null;
    }

    @Override
    public CourseWeb selectInfoWebById(String id) {
        return null;
    }

    /**
     * 1 根据条件查询课程信息
     * @param pageParam
     * @param courseQuery
     * @return
     */
    @Override
    public IPage<CourseInfo> pageQuery(Page<CourseInfo> pageParam, CourseQuery courseQuery) {

        // 1 当查询条件为空时
        if (courseQuery == null){
            return baseMapper.selectPage(pageParam,null);
        }
        // 2 不为空时
        // 3. 取出各个条件，进行拼接
        String title = courseQuery.getTitle();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();
        String teacherId = courseQuery.getTeacherId();

        QueryWrapper<CourseInfo> wrapper = new QueryWrapper<>();
        if ( !StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(subjectId)) {
            wrapper.eq("subject_id", subjectId);
        }
        if ( !StringUtils.isEmpty(teacherId)) {
            wrapper.eq("teacher_id", teacherId);
        }
        return baseMapper.selectPage(pageParam, wrapper);
    }

    /**
     * 保存课程信息
     * @param courseInfoDTO
     * @return 课程id
     */
    @Override
    public String saveCourseInfo(CourseInfoDTO courseInfoDTO) {

        // 1.新建course对象
        CourseInfo CourseInfo = new CourseInfo();
        // 2.将传输对象的内容复制给新建对象
        BeanUtils.copyProperties(courseInfoDTO, CourseInfo);
        // 3.保存课程
        int result = baseMapper.insert(CourseInfo);
        if (result == 0){
            throw new Exception(ResultEnum.SAVE_COURSE_FALSE); //保存课程失败异常
        }
        // 4.新建课程描述对象,并设置参数
        CourseDescription CourseDescription = new CourseDescription();
        CourseDescription.setId(CourseInfo.getId());
        CourseDescription.setDescription(courseInfoDTO.getDescription());
        System.out.println(CourseDescription);

        // 5.保存课程描述到数据库
        boolean save = CourseDescriptionService.save(CourseDescription);

        if (save){
            return CourseInfo.getId();
        }else {
            return null;
        }

    }

    /**
     *根据课程id查询课程信息
     * @param id
     * @return
     */
    @Override
    public CourseInfoDTO findCourseById(String id) {
        //新建一个课程信息传输对象
        CourseInfoDTO courseInfoDTO = new CourseInfoDTO();
        //查询课程基本信息
        CourseInfo CourseInfo = baseMapper.selectById(id);

        if (CourseInfo == null){
            throw new Exception(ResultEnum.COURSE_NOT_EXIST);
        }
        //将查询到的信息复制到传输对象中
        BeanUtils.copyProperties(CourseInfo, courseInfoDTO);

        //查询课程描述
        CourseDescription CourseDescription = CourseDescriptionService.getById(id);

        //将查询到的信息复制到传输对象中
       courseInfoDTO.setDescription(CourseDescription.getDescription());

        return courseInfoDTO;
    }

    @Override
    public Boolean updateCourse(CourseInfoDTO courseInfoDTO) {
        //1 修改课程基本信息表
        CourseInfo CourseInfo = new CourseInfo();
        BeanUtils.copyProperties(courseInfoDTO,CourseInfo);
        int result = baseMapper.updateById(CourseInfo);
        if(result == 0) {
            throw new Exception(ResultEnum.UPDATE_COURSE_FALSE);
        }

        //2 修改描述表
        CourseDescription CourseDescription = new CourseDescription();
        String id = courseInfoDTO.getId();
        String description = courseInfoDTO.getDescription();
        CourseDescription.setId(id);
        CourseDescription.setDescription(description);
        boolean b = CourseDescriptionService.updateById(CourseDescription);
        return b;
    }

    //根据课程id查询课程详情
    @Override
    public CourseInfo getCourseInfoAll(String courseId) {
        CourseInfo courseInfoAll = baseMapper.getCourseInfoById(courseId);
        return courseInfoAll;
    }

    @Override
    public boolean removeCourseId(String id) {
        // 根据课程id删除章节
        ChapterService.deleteChapterByCourseId(id);

        // 根据课程id删除课程描述
        CourseDescriptionService.deleteDescriptionByCourseId(id);

        // 删除课程本身
        int result = baseMapper.deleteById(id);
        return result>0;
    }
}
